package cn.yuanqiao.web.controller.archive.appManager;


import cn.yuanqiao.archive.appManager.service.ITenantFondsService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.manage.domain.ManageFonds;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/archive/fondscode")
public class TenantFondsController extends BaseController {

    @Autowired
    private ITenantFondsService tenantFondsService;

    @GetMapping("/list")
    @Log(title = "查询全宗管理列表",businessType = BusinessType.SELECT)
    public TableDataInfo list(ManageFonds manageFonds)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        manageFonds.setTenantCode(tenantCode);
        startPage();
        List<ManageFonds> list = tenantFondsService.selectManageFondsList(manageFonds);
        return getDataTable(list);
    }

    @GetMapping("/treelist")
    public AjaxResult treelist()
    {
        return success(tenantFondsService.treeList());
    }

    /**
     * 导出租户全宗管理列表
     */
    //@RequiresPermissionsssions("manage:fonds:export")
    //@Log(title = "租户全宗管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ManageFonds manageFonds)
    {
        List<ManageFonds> list = tenantFondsService.selectManageFondsList(manageFonds);
        ExcelUtil<ManageFonds> util = new ExcelUtil<ManageFonds>(ManageFonds.class);
        util.exportExcel(response, list, "租户全宗管理数据");
    }

    /**
     * 获取租户全宗管理详细信息
     */
    //@RequiresPermissionsssions("manage:fonds:query")
    @Log(title = "获取全宗管理详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(tenantFondsService.selectManageFondsByID(ID));
    }

    /**
     * 新增租户全宗管理
     */
    //@RequiresPermissionsssions("manage:fonds:add")
    @Log(title = "新增全宗管理信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ManageFonds manageFonds)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        manageFonds.setTenantCode(tenantCode);
        return toAjax(tenantFondsService.insertManageFonds(manageFonds));
    }

    /**
     * 修改租户全宗管理
     */
    //@RequiresPermissionsssions("manage:fonds:edit")
    @Log(title = "修改全宗管理信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ManageFonds manageFonds)
    {
        return toAjax(tenantFondsService.updateManageFonds(manageFonds));
    }

    /**
     * 删除租户全宗管理
     */
    //@RequiresPermissionsssions("manage:fonds:remove")
    @Log(title = "删除全宗管理信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(tenantFondsService.deleteManageFondsByIDs(IDs));
    }

    //@RequiresPermissionsssions("manage:fonds:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String tenantId) throws Exception
    {
        ExcelUtil<ManageFonds> util = new ExcelUtil<>(ManageFonds.class);
        List<ManageFonds> manageList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = tenantFondsService.importFonds(manageList, updateSupport, operName,tenantId);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<ManageFonds> util = new ExcelUtil<ManageFonds>(ManageFonds.class);
        util.importTemplateExcel(response, "租户全宗数据");
    }

    @Log(title = "初始化修改全宗", businessType = BusinessType.UPDATE)
    @PostMapping("/initFonds")
    public AjaxResult initFonds(@RequestBody JSONObject object)
    {
        if(StringUtils.isEmpty(object.getString("fondsName")) || StringUtils.isEmpty(object.getString("fondsCode"))){
            throw new ServiceException("参数错误");
        }
        tenantFondsService.initFonds(object.getString("fondsCode"),object.getString("fondsName"));
        return AjaxResult.success();
    }

}
